package solvoku.model.game;

import java.util.HashSet;
import solvoku.model.game.utils.PossibilitesCase;

/**
 *
 * @author Steve Cancès <steve.cances@gmail.com>
 */
public class Case implements Comparable<Case> {

    private static final int COMPARE_TO_LESS = -10;
    private static final int COMPARE_TO_EQUAL = 0;
    private static final int COMPARE_TO_MORE = 10;

    /**
     * La valeur de la case (0 si vide, sinon de 1 à 9)
     */
    private int value;
    /**
     * Les possibilites de la case
     */
    private HashSet<Integer> possibilites;
    /**
     * coordonnée de la case en x
     */
    private int x;
    /**
     * coordonnée de la case en y
     */
    private int y;
    /**
     * La ligne ou se trouve la case
     */
    private GroupeCase ligne;
    /**
     * La colonne ou se trouve la case
     */
    private GroupeCase colonne;
    /**
     * Le bloc ou se trouve la case
     */
    private GroupeCase bloc;
    /**
     * La grille ou se trouve la case
     */
    private Grille grille;

    public Case() {
        this(0, 0);
    }

    public Case(int x, int y) {
        super();
        this.value = 0;
        this.possibilites = PossibilitesCase.getPossibilitesCase();
        this.x = x;
        this.y = y;
        this.ligne = null;
        this.colonne = null;
        this.bloc = null;
        this.grille = null;
    }

    /**
     * @return the value
     */
    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        if (value > 0 && value <= 9) {
            this.value = value;
            this.possibilites.clear();
        }
    }

    public void unsetValue() {
        this.setValue(0);
        this.possibilites = PossibilitesCase.getPossibilitesCase();
    }

    public HashSet<Integer> getPossibilites() {
        return possibilites;
    }

    public boolean addPossibilite(Integer possibilite) {
        return possibilites.add(possibilite);
    }

    public boolean removePossibilite(Integer possibilite) {
        return possibilites.remove(possibilite);
    }

    public int nbPossibilites() {
        return possibilites.size();
    }

    /**
     * @return the x
     */
    public int getX() {
        return x;
    }

    /**
     * @param x the x to set
     */
    public void setX(int x) {
        this.x = x;
    }

    /**
     * @return the y
     */
    public int getY() {
        return y;
    }

    /**
     * @param y the y to set
     */
    public void setY(int y) {
        this.y = y;
    }

    /**
     * @return the ligne
     */
    public GroupeCase getLigne() {
        return ligne;
    }

    /**
     * @param ligne the ligne to set
     */
    public void setLigne(GroupeCase ligne) {
        this.ligne = ligne;
    }

    /**
     * @return the colonne
     */
    public GroupeCase getColonne() {
        return colonne;
    }

    /**
     * @param colonne the colonne to set
     */
    public void setColonne(GroupeCase colonne) {
        this.colonne = colonne;
    }

    /**
     * @return the bloc
     */
    public GroupeCase getBloc() {
        return bloc;
    }

    /**
     * @param bloc the bloc to set
     */
    public void setBloc(GroupeCase bloc) {
        this.bloc = bloc;
    }

    /**
     * @return the grille
     */
    public Grille getGrille() {
        return grille;
    }

    /**
     * @param grille the grille to set
     */
    public void setGrille(Grille grille) {
        this.grille = grille;
    }

    @Override
    public int compareTo(Case c) {
        if (this.x == c.x) {
            if (this.y == c.y) {
                return COMPARE_TO_EQUAL;
            } else if (this.y < c.y) {
                return COMPARE_TO_LESS;
            } else {
                return COMPARE_TO_MORE;
            }
        } else if (this.x < c.x) {
            return COMPARE_TO_LESS;
        } else {
            return COMPARE_TO_MORE;
        }
    }

    @Override
    public boolean equals(Object object) {
        if (object != null && object instanceof Case) {
            Case c = (Case) object;
            return (this.x == c.x && this.y == c.y);
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + this.x;
        hash = 59 * hash + this.y;
        return hash;
    }

    public boolean isVide() {
        return this.value == 0;
    }

    @Override
    public String toString() {
        return "Case{" + "value=" + value + ", possibilites=" + possibilites + ", x=" + x + ", y=" + y + '}';
    }
    
    
}
